package trocabi.DAO;

import trocabi.Socle.DBManager;
import trocabi.model.Article;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Logger;
import trocabi.Socle.StringTools;
import trocabi.Socle.TechnicalException;

public class DAOArticle extends DAO<ArrayList<Article>, Article> {

    private static final Logger logger = Logger.getLogger("trocabi.DAO.DAOArticle");
    public static final String TB_ARTICLE        = "ARTICLE";
    public static final String FD_ID             = "CODE_ARTICLE";
    public static final String FD_ID_DEPOSITAIRE = "NUMERO_DEPOSITAIRE";
    public static final String FD_ASPECT         = "ASPECT";
    public static final String FD_TYPE           = "TYPE";
    public static final String FD_GENRE          = "GENRE";
    public static final String FD_COULEUR        = "COULEUR"; 
    public static final String FD_STATUS         = "SITUATION_DEPOT";
    public static final String FD_COMMENTAIRE    = "COMMENTAIRE"; 
    public static final String FD_CODECAISSE     = "CODE_CAISSE";
    public static final String FD_PRIX_ACHAT     = "PRIX_ACHAT";
    public static final String FD_TX_MARGE       = "TX_MARGE";
    public static final String FD_DESCRIPTION    = "DESCRIPTION";
    public static final String FD_DATE_DEPOT     = "DATE_DEPOT";
    public static final String FD_DATE_PAIEMENT  = "DATE_PAIEMENT";
    public static final String FD_DATE_ACHAT     = "DATE_ACHAT";
    public static final String FD_DATE_LASTSTATE = "DATE_LASTSTATE";
  
    
    
    //Variable nécessaire aux requêtes SQL
    private static String sSqlQuery;
    public static final String sEGAL_STRICT = " = ";
    public static final String sINFERIEUR_STRICT = " < ";
    public static final String sSUPERIEUR_STRICT = " > ";
    public static final String sINFERIEUR = " <= ";
    public static final String sSUPERIEUR = " >=";
    public static final String sCONTENANT = " LIKE ";
    public static final String sDIFFERENT = " <> ";
    public static final String sCROISSANT = " ASC ";
    public static final String sDECROISSANT = " DESC ";
    //Varibles liées aux conditions de mises à jour et de sélection
    public static final int iID_DB = 0;
    public static final int iVALEUR = 1;
    public static final int iMODE_COMPARAISON = 2;
    public static final int iMODE_TRIE = 3;
    public static final int iINFO_AFFICHEE = 0;
    public static final int iDISTINCT = 1;
    public static final String sDISTINCT_OK = "OK";
    public static final String sNO_DISTINCT = "NO";
    public static final int iNO_LIMIT = 0;
    private static DAOArticle instance = null;

    private DAOArticle() {
    }

    public static synchronized DAOArticle getInstance() {
        if (instance == null) {
            instance = new DAOArticle();
        }
        return instance;
    }

    @Override
    public Article get(long id) throws TechnicalException { // TODO: implement it 
        Article article=null;
        Statement   statement;

        try { 
            statement = DBManager.getConnection().createStatement();
            String query=String.format("SELECT * FROM %s WHERE %s=%d",TB_ARTICLE,FD_ID,id);
            ResultSet rsResult = statement.executeQuery(query);
            if (rsResult.first()) {
                   article = new Article(rsResult.getLong(FD_ID),
                            rsResult.getLong(FD_ID_DEPOSITAIRE),
                            StringTools.unEscape(rsResult.getString(FD_DESCRIPTION)),
                            rsResult.getString(FD_ASPECT),
                            rsResult.getString(FD_GENRE),
                            rsResult.getString(FD_TYPE),
                            rsResult.getString(FD_COULEUR), 
                            Article.codeToEtat(rsResult.getString(FD_STATUS)),
                            StringTools.unEscape(rsResult.getString(FD_COMMENTAIRE)),
                            rsResult.getString(FD_CODECAISSE),
                            rsResult.getDouble(FD_PRIX_ACHAT),
                            rsResult.getDouble(FD_TX_MARGE),
                            DBManager.sqlDateToDate(rsResult.getDate(FD_DATE_DEPOT)),
                            DBManager.sqlDateToDate(rsResult.getDate(FD_DATE_PAIEMENT)),
                            DBManager.sqlDateToDate(rsResult.getDate(FD_DATE_ACHAT)),
                            DBManager.sqlDateToDate(rsResult.getDate(FD_DATE_LASTSTATE)));
            }
        } catch (SQLException | TechnicalException e) {
            logger.severe(String.format("%s %s",e.getLocalizedMessage(),e.getMessage()));
            throw new TechnicalException(e.getMessage(),e.getCause());
        }
        return article;
    }
    
    @Override
    public long insert(Article article) throws TechnicalException {
        Statement statement;
        long id;

        try { // forcer valeur null dans un insert!?
            statement = DBManager.getConnection().createStatement(); // TODO: String avec '
            java.sql.Date dateDepot=DBManager.dateToSqlDate(article.getDateDepot());
            java.sql.Date datePaiement=DBManager.dateToSqlDate(article.getDatePaiementDepositaire());
            java.sql.Date dateAchat=DBManager.dateToSqlDate(article.getDateAchat());
            java.sql.Date dateChEtat=DBManager.dateToSqlDate(article.getDateChangementEtat());
                            
            String query = String.format("INSERT INTO %s (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) VALUES ('%s','%s','%s','%s','%s','%s','%s',%s,'%s','%s','%s',%s,%s,%s,%s);",
                    TB_ARTICLE,
                    FD_ID_DEPOSITAIRE,FD_ASPECT,FD_TYPE,FD_GENRE,FD_COULEUR,
                    FD_STATUS,FD_COMMENTAIRE,FD_CODECAISSE,FD_PRIX_ACHAT,
                    FD_TX_MARGE,FD_DESCRIPTION,FD_DATE_DEPOT, FD_DATE_PAIEMENT,
                    FD_DATE_ACHAT, FD_DATE_LASTSTATE,
                    Long.toString(article.getIdDepositaire()), 
                    article.getCodeAspect(),article.getCodeType(), article.getCodeGenre(),article.getCouleur(),
                    Article.etatToCode(article.getSituation()),
                    StringTools.escape(article.getCommentaire()),
                    article.getCodeCaisse()==null?"NULL":"'"+article.getCodeCaisse()+"'",
                    Double.toString(article.getPrixAchat()),
                    article.getTxMarge(), StringTools.escape(article.getDescription()),
                    dateDepot==null?"NULL":"'"+dateDepot.toString()+"'",
                    datePaiement==null?"NULL":"'"+datePaiement.toString()+"'",
                    dateAchat==null?"NULL":"'"+dateAchat.toString()+"'",
                    dateChEtat==null?"NULL":"'"+dateChEtat.toString()+"'");
            id = statement.executeUpdate(query, Statement.RETURN_GENERATED_KEYS);
            ResultSet rs = statement.getGeneratedKeys();
            if (rs.next()) {
                id = rs.getInt(1);
            }
            DBManager.getConnection().commit();
        } catch (SQLException | TechnicalException e) {
            logger.severe(String.format("%s %s", e.getLocalizedMessage(), e.getMessage()));
            throw new TechnicalException(e.getMessage(), e.getCause());
        }
        return id;
    }

    @Override
    public void update(Article article) throws TechnicalException {
        Statement statement;
        try {
            statement = DBManager.getConnection().createStatement();
            java.sql.Date dateDepot=DBManager.dateToSqlDate(article.getDateDepot());
            java.sql.Date datePaiement=DBManager.dateToSqlDate(article.getDatePaiementDepositaire());
            java.sql.Date dateAchat=DBManager.dateToSqlDate(article.getDateAchat());
            java.sql.Date dateChEtat=DBManager.dateToSqlDate(article.getDateChangementEtat());
            /*
            String query = String.format("UDATE  %s SET %s='%d',%s='%s',%s='%s',%s='%s',%s='%s',%s='%s',%s='%s',%s='%s',"
                                       + "%s=%s,%s='%s',%s=%s,%s=%s,%s=%s,%s=%s WHERE %s=%d;",
                    TB_ARTICLE,
                    FD_ID_DEPOSITAIRE, article.getIdDepositaire(), 
                    FD_DESCRIPTION,    StringTools.escape(article.getDescription()),
                    FD_ASPECT,         article.getCodeAspect(),    FD_TYPE, article.getCodeType(),
                    FD_GENRE,          article.getCodeGenre(),     FD_COULEUR, article.getCouleur(),
                    FD_STATUS,         Article.etatToCode(article.getSituation()),     
                    FD_COMMENTAIRE,    StringTools.escape(article.getCommentaire()),
                    FD_CODECAISSE,     article.getCodeCaisse()==null?"NULL":"'"+article.getCodeCaisse()+"'",    
                    FD_TX_MARGE,       Double.toString(article.getTxMarge()),
                    FD_PRIX_ACHAT,     Double.toString(article.getPrixAchat()),     
                    FD_DATE_DEPOT,     dateDepot==null?"NULL":"'"+dateDepot.toString()+"'",
                    FD_DATE_PAIEMENT,  datePaiement==null?"NULL":"'"+datePaiement.toString()+"'",
                    FD_DATE_ACHAT,     dateAchat==null?"NULL":"'"+dateAchat.toString()+"'",
                    FD_DATE_LASTSTATE, dateChEtat==null?"NULL":"'"+dateChEtat.toString()+"'",
                    FD_ID,  article.getId());  
            */
            String query1 = String.format("UPDATE  %s SET %s=%d,%s='%s',%s='%s',%s='%s',%s='%s',%s='%s',%s='%s',%s='%s'", 
                    TB_ARTICLE,
                    FD_ID_DEPOSITAIRE, article.getIdDepositaire(), 
                    FD_DESCRIPTION,    StringTools.escape(article.getDescription()),
                    FD_ASPECT,         article.getCodeAspect(),    FD_TYPE, article.getCodeType(),
                    FD_GENRE,          article.getCodeGenre(),     FD_COULEUR, article.getCouleur(),
                    FD_STATUS,         Article.etatToCode(article.getSituation()),     
                    FD_COMMENTAIRE,    StringTools.escape(article.getCommentaire()));
            String query2 = String.format(",%s=%s,%s='%s',%s='%s',%s=%s,%s=%s,%s=%s,%s=%s WHERE %s=%d;",
                    FD_CODECAISSE,     article.getCodeCaisse()==null?"NULL":"'"+article.getCodeCaisse()+"'",    
                    FD_TX_MARGE,       Double.toString(article.getTxMarge()),
                    FD_PRIX_ACHAT,     Double.toString(article.getPrixAchat()),     
                    FD_DATE_DEPOT,     dateDepot==null?"NULL":"'"+dateDepot.toString()+"'", 
                    FD_DATE_PAIEMENT,  datePaiement==null?"NULL":"'"+datePaiement.toString()+"'",
                    FD_DATE_ACHAT,     dateAchat==null?"NULL":"'"+dateAchat.toString()+"'",
                    FD_DATE_LASTSTATE, dateChEtat==null?"NULL":"'"+dateChEtat.toString()+"'",
                    FD_ID,  article.getId());  
            System.out.println(query1+query2);
            long res= statement.executeUpdate(query1+query2, Statement.RETURN_GENERATED_KEYS);
            DBManager.getConnection().commit();

        } catch (SQLException | TechnicalException e) {
            logger.severe(String.format("%s %s", e.getLocalizedMessage(), e.getMessage()));
            throw new TechnicalException(e.getMessage(), e.getCause());
        }
    }

    public ArrayList<Article> searchArticles(String query) throws TechnicalException {
        ArrayList<Article> listListe_Article = new <Article>ArrayList();
        Statement statement;

        try {
            statement = DBManager.getConnection().createStatement();
            ResultSet rsResult = statement.executeQuery(query);
            if (rsResult.first()) {
                listListe_Article = new <Article>ArrayList();
                do {
                    Article article = new Article(rsResult.getLong(FD_ID),
                            rsResult.getLong(FD_ID_DEPOSITAIRE),
                            StringTools.unEscape(rsResult.getString(FD_DESCRIPTION)),
                            rsResult.getString(FD_ASPECT),
                            rsResult.getString(FD_GENRE),
                            rsResult.getString(FD_TYPE),
                            rsResult.getString(FD_COULEUR), 
                            Article.codeToEtat(rsResult.getString(FD_STATUS)),
                            StringTools.unEscape(rsResult.getString(FD_COMMENTAIRE)),
                            rsResult.getString(FD_CODECAISSE),
                            rsResult.getDouble(FD_PRIX_ACHAT),
                            rsResult.getDouble(FD_TX_MARGE),
                            DBManager.sqlDateToDate(rsResult.getDate(FD_DATE_DEPOT)),
                            DBManager.sqlDateToDate(rsResult.getDate(FD_DATE_PAIEMENT)),
                            DBManager.sqlDateToDate(rsResult.getDate(FD_DATE_ACHAT)),
                            DBManager.sqlDateToDate(rsResult.getDate(FD_DATE_LASTSTATE)));
                    listListe_Article.add(article);
                } while (rsResult.next());
            }
        } catch (SQLException | TechnicalException e) {
            logger.severe(String.format("%s %s", e.getLocalizedMessage(), e.getMessage()));
            throw new TechnicalException(e.getMessage(), e.getCause());
        }
        return listListe_Article;
    }
/*
    public void update(Article article) {
        Statement statement;
        String query = "";

        try {
            query = String.format("UPDATE ARTICLE SET %s = '%s', %s = %s WHERE %s = %d;",
                    FD_DATE_CHT, (new java.sql.Date(article.getDateChangementEtat().getTime())).toString(),
                    FD_STATUS, article.getSituation(),
                    FD_ID, article.getId());
            statement = DBManager.getConnection().createStatement();
            statement.executeUpdate(query, Statement.RETURN_GENERATED_KEYS);
            DBManager.getConnection().commit();
        } catch (SQLException | TechnicalException e) {
            logger.severe(String.format("%s %s query=[%s]", e.getLocalizedMessage(), e.getMessage(), query));
        }
    }

    @Override
    public boolean bCreate(Article artArticle) {
        Statement statement;

        try {
            int iRetour;
            if (artArticle.getId() < 0 || artArticle.getIdDepositaire() < 0
                    || artArticle.getPrixAchat() < 0 || artArticle.getMarge() < 0) {
                return false;
            }
            statement = DBManager.getConnection().createStatement();

            sSqlQuery = "INSERT INTO " + TB_ARTICLE + " ( " + FD_ID + "," + FD_ID_DEPOSITAIRE + ",";
            //Argument facultatif
            if (artArticle.getDescription() != null) {
                sSqlQuery = sSqlQuery + FD_DESCRIPTION + ",";
            }
            sSqlQuery = sSqlQuery + FD_ASPECT + "," + FD_TYPE + "," + sID_DB_POUR + "," + sID_DB_COULEUR + "," + FD_STATUS + ",";
            //Argument facultatif
            if (artArticle.getDateChangementEtat() != null) {
                sSqlQuery = sSqlQuery + FD_DATE_CHT + ",";
            }
            //Argument facultatif
            if (artArticle.getCommentaire() != null) {
                sSqlQuery = sSqlQuery + FD_COMMENTAIRE + ",";
            }
            //Argument facultatif
            if (artArticle.getIdCaisse() != null) {
                sSqlQuery = sSqlQuery + FD_CAISSEID + ",";
            }
            sSqlQuery = sSqlQuery + FD_DATE_DEPOT + "," + FD_PRIX_ACHAT + "," + FD_MARGE + ") VALUES ("
                    + artArticle.getId() + "," + artArticle.getIdDepositaire() + ",'";
            if (artArticle.getDescription() != null) {
                sSqlQuery = sSqlQuery + artArticle.getDescription() + "','";
            }
            sSqlQuery = sSqlQuery + artArticle.getCodeAspect() + "','" + artArticle.getCodeType() + "','"
                    + artArticle.getCodeGenre() + "','" + artArticle.getCouleur() + "','"
                    + artArticle.getSituation() + "','";
            //Argument facultatif
            if (artArticle.getDateChangementEtat() != null) {
                sSqlQuery = sSqlQuery + artArticle.getDateChangementEtat() + "','";
            }
            //Argument facultatif
            if (artArticle.getCommentaire() != null) {
                sSqlQuery = sSqlQuery + artArticle.getCommentaire() + "','";
            }
            //Argument facultatif
            if (artArticle.getIdCaisse() != null) {
                sSqlQuery = sSqlQuery + artArticle.getIdCaisse() + "','";
            }
            sSqlQuery = sSqlQuery + artArticle.getDateDepot() + "'," + artArticle.getPrixAchat() + ","
                    + artArticle.getMarge() + ");";
            iRetour = statement.executeUpdate(sSqlQuery, Statement.RETURN_GENERATED_KEYS);
            DBManager.getConnection().commit();
            if (iRetour != 1) {
                return false;
            }
            return true;
        } catch (Exception exException) {
            return false;
        }
    }
*/
    @Override
    public boolean bDelete(long iCode_Article) {
        Statement statement = null;
        int iRetour;
        try {

            statement = DBManager.getConnection().createStatement();
            sSqlQuery = "DELETE FROM " + TB_ARTICLE + " WHERE " + FD_ID + " = '" + iCode_Article + "';";
            iRetour = statement.executeUpdate(sSqlQuery, Statement.RETURN_GENERATED_KEYS);
            DBManager.getConnection().commit();
            if (iRetour != 1) {
                return false;
            }
            return true;
        } catch (Exception exException) {
            return false;
        }
    }

    /**
     *
     * @param listListeUpdate
     * @param listListeConditions
     * @return
     */
    /*
    public boolean bUpdate(ArrayList<String[]> listListeUpdate, ArrayList<String[]> listListeConditions) throws TechnicalException {
        Statement statement;
        int ilongueur_Minimale;
        int iRetour;
        try {
            if (listListeUpdate == null || listListeUpdate.isEmpty() || listListeConditions == null || listListeConditions.isEmpty()) {
                return false;
            }
            statement = DBManager.getConnection().createStatement();
            sSqlQuery = "UPDATE " + TB_ARTICLE + " SET ";
            ilongueur_Minimale = sSqlQuery.length();
            while (listListeUpdate.size() > 0) {
                if (listListeUpdate.get(0)[iID_DB].equals(FD_ID)) {
                    return false;
                }
                if (sSqlQuery.length() == ilongueur_Minimale) {
                    if (listListeUpdate.get(0)[iID_DB].equals(FD_ID_DEPOSITAIRE) || listListeUpdate.get(0)[iID_DB].equals(FD_PRIX_ACHAT) || listListeUpdate.get(0)[iID_DB].equals(FD_MARGE)) {
                        sSqlQuery = sSqlQuery + listListeUpdate.get(0)[iID_DB] + " = " + listListeUpdate.get(0)[iVALEUR];
                    } else {
                        sSqlQuery = sSqlQuery + listListeUpdate.get(0)[iID_DB] + " = '" + listListeUpdate.get(0)[iVALEUR] + "'";
                    }
                    listListeUpdate.remove(0);
                } else {
                    if (listListeUpdate.get(0)[iID_DB].equals(FD_ID_DEPOSITAIRE) || listListeUpdate.get(0)[iID_DB].equals(FD_PRIX_ACHAT) || listListeUpdate.get(0)[iID_DB].equals(FD_MARGE)) {
                        sSqlQuery = sSqlQuery + ", " + listListeUpdate.get(0)[iID_DB] + " = " + listListeUpdate.get(0)[iVALEUR];
                    } else {
                        sSqlQuery = sSqlQuery + ", " + listListeUpdate.get(0)[iID_DB] + " = '" + listListeUpdate.get(0)[iVALEUR] + "'";
                    }
                    listListeUpdate.remove(0);
                }
            }
            ilongueur_Minimale = sSqlQuery.length();
            while (listListeConditions.size() > 0) {
                if (listListeConditions.get(0)[iMODE_COMPARAISON] != null) {
                    if (sSqlQuery.length() == ilongueur_Minimale) {
                        if (listListeConditions.get(0)[iID_DB].equals(FD_ID) || listListeConditions.get(0)[iID_DB].equals(FD_ID_DEPOSITAIRE)
                                || listListeConditions.get(0)[iID_DB].equals(FD_PRIX_ACHAT) || listListeConditions.get(0)[iID_DB].equals(FD_MARGE)) {
                            if (listListeConditions.get(0)[iMODE_COMPARAISON].equals(sCONTENANT)) {
                                return false;
                            }
                            sSqlQuery = sSqlQuery + " WHERE " + listListeConditions.get(0)[iID_DB] + listListeConditions.get(0)[iMODE_COMPARAISON] + listListeConditions.get(0)[iVALEUR];
                        } else {
                            if (listListeConditions.get(0)[iMODE_COMPARAISON].equals(sCONTENANT)) {
                                sSqlQuery = sSqlQuery + " WHERE " + listListeConditions.get(0)[iID_DB] + listListeConditions.get(0)[iMODE_COMPARAISON] + "'%" + listListeConditions.get(0)[iVALEUR] + "%'";
                            } else {
                                sSqlQuery = sSqlQuery + " WHERE " + listListeConditions.get(0)[iID_DB] + listListeConditions.get(0)[iMODE_COMPARAISON] + "'" + listListeConditions.get(0)[iVALEUR] + "'";
                            }
                        }
                        listListeConditions.remove(0);
                    } else {
                        if (listListeConditions.get(0)[iID_DB].equals(FD_ID) || listListeConditions.get(0)[iID_DB].equals(FD_ID_DEPOSITAIRE)
                                || listListeConditions.get(0)[iID_DB].equals(FD_PRIX_ACHAT) || listListeConditions.get(0)[iID_DB].equals(FD_MARGE)) {
                            if (listListeConditions.get(0)[iMODE_COMPARAISON].equals(sCONTENANT)) {
                                return false;
                            }
                            sSqlQuery = sSqlQuery + " AND " + listListeConditions.get(0)[iID_DB] + listListeConditions.get(0)[iMODE_COMPARAISON] + listListeConditions.get(0)[iVALEUR];
                        } else {
                            if (listListeConditions.get(0)[iMODE_COMPARAISON].equals(sCONTENANT)) {
                                sSqlQuery = sSqlQuery + " AND " + listListeConditions.get(0)[iID_DB] + listListeConditions.get(0)[iMODE_COMPARAISON] + "'%" + listListeConditions.get(0)[iVALEUR] + "%'";
                            } else {
                                sSqlQuery = sSqlQuery + " AND " + listListeConditions.get(0)[iID_DB] + listListeConditions.get(0)[iMODE_COMPARAISON] + "'" + listListeConditions.get(0)[iVALEUR] + "'";
                            }
                        }
                        listListeConditions.remove(0);
                    }
                } else {
                    return false;
                }
            }
            sSqlQuery = sSqlQuery + ";";
            iRetour = statement.executeUpdate(sSqlQuery, Statement.RETURN_GENERATED_KEYS);
            if (iRetour != 1) {
                return false;
            }
            DBManager.getConnection().commit();

        } catch (SQLException e) {
            throw new TechnicalException(e.getMessage(), e.getCause());
        }
        return true;
    }
*/
    @Override
    public ArrayList<Article> listSelect(ArrayList<String[]> listListeConditions, ArrayList<String[]> listListeInfos_Affichees, int limite) throws TechnicalException {
        int ilongueur_Minimale;
        sSqlQuery = "SELECT";
        if (listListeInfos_Affichees == null || listListeInfos_Affichees.isEmpty()) {
            sSqlQuery = sSqlQuery + " *";
        } else {
            int iIndice = 0;
            ArrayList<String[]> listListeInfos_Affichees_Distinctes = new <String[]>ArrayList();
            while (iIndice <= listListeInfos_Affichees.size()) {
                if (listListeInfos_Affichees.get(iIndice)[iDISTINCT].equals(sDISTINCT_OK)) {
                    listListeInfos_Affichees_Distinctes.add(listListeInfos_Affichees.get(iIndice));
                    listListeInfos_Affichees.remove(iIndice);
                }
                iIndice++;
            }
            if (listListeInfos_Affichees_Distinctes.isEmpty() == false) {
                sSqlQuery = sSqlQuery + " DISTINCT (";
                ilongueur_Minimale = sSqlQuery.length();
                while (listListeInfos_Affichees_Distinctes.size() > 0) {
                    if (sSqlQuery.length() == ilongueur_Minimale) {
                        sSqlQuery = sSqlQuery + listListeInfos_Affichees_Distinctes.get(0)[iINFO_AFFICHEE];
                        listListeInfos_Affichees_Distinctes.remove(0);
                    } else {
                        sSqlQuery = sSqlQuery + ", " + listListeInfos_Affichees_Distinctes.get(0)[iINFO_AFFICHEE];
                        listListeInfos_Affichees_Distinctes.remove(0);
                    }
                }
                sSqlQuery = sSqlQuery + " )";
                if (listListeInfos_Affichees.isEmpty() == false) {
                    sSqlQuery = sSqlQuery + ",";
                }
            }
            ilongueur_Minimale = sSqlQuery.length();
            while (listListeInfos_Affichees.size() > 0) {
                if (sSqlQuery.length() == ilongueur_Minimale) {
                    sSqlQuery = sSqlQuery + " " + listListeInfos_Affichees.get(0)[iINFO_AFFICHEE];
                    listListeInfos_Affichees.remove(0);
                } else {
                    sSqlQuery = sSqlQuery + ", " + listListeInfos_Affichees.get(0)[iINFO_AFFICHEE];
                    listListeInfos_Affichees.remove(0);
                }
            }
        }
        sSqlQuery = sSqlQuery + " FROM " + TB_ARTICLE;
        ilongueur_Minimale = sSqlQuery.length();
        int iI = 0;
        while (listListeConditions != null && iI < listListeConditions.size()) {
            if (listListeConditions.get(iI)[iMODE_COMPARAISON] != null) {
                if (sSqlQuery.length() == ilongueur_Minimale) {
                    if (listListeConditions.get(iI)[iID_DB].equals(FD_ID) || listListeConditions.get(iI)[iID_DB].equals(FD_ID_DEPOSITAIRE)
                            || listListeConditions.get(iI)[iID_DB].equals(FD_PRIX_ACHAT) || listListeConditions.get(iI)[iID_DB].equals(FD_TX_MARGE)) {
                        if (listListeConditions.get(iI)[iMODE_COMPARAISON].equals(sCONTENANT)) {
                            return null;
                        }
                        sSqlQuery = sSqlQuery + " WHERE " + listListeConditions.get(iI)[iID_DB] + listListeConditions.get(iI)[iMODE_COMPARAISON] + listListeConditions.get(iI)[iVALEUR];
                    } else {
                        if (listListeConditions.get(iI)[iMODE_COMPARAISON].equals(sCONTENANT)) {
                            sSqlQuery = sSqlQuery + " WHERE " + listListeConditions.get(iI)[iID_DB] + listListeConditions.get(iI)[iMODE_COMPARAISON] + "'%" + listListeConditions.get(iI)[iVALEUR] + "%'";
                        } else {
                            sSqlQuery = sSqlQuery + " WHERE " + listListeConditions.get(iI)[iID_DB] + listListeConditions.get(iI)[iMODE_COMPARAISON] + "'" + listListeConditions.get(iI)[iVALEUR] + "'";
                        }
                    }
                    if (listListeConditions.get(iI)[iMODE_TRIE] == null) {
                        listListeConditions.remove(iI);
                    } else {
                        iI++;
                    }
                } else {
                    if (listListeConditions.get(iI)[iID_DB].equals(FD_ID) || listListeConditions.get(iI)[iID_DB].equals(FD_ID_DEPOSITAIRE)
                            || listListeConditions.get(iI)[iID_DB].equals(FD_PRIX_ACHAT) || listListeConditions.get(iI)[iID_DB].equals(FD_TX_MARGE)) {
                        if (listListeConditions.get(iI)[iMODE_COMPARAISON].equals(sCONTENANT)) {
                            return null;
                        }
                        sSqlQuery = sSqlQuery + " AND " + listListeConditions.get(iI)[iID_DB] + listListeConditions.get(iI)[iMODE_COMPARAISON] + listListeConditions.get(iI)[iVALEUR];
                    } else {
                        if (listListeConditions.get(iI)[iMODE_COMPARAISON].equals(sCONTENANT)) {
                            sSqlQuery = sSqlQuery + " AND " + listListeConditions.get(iI)[iID_DB] + listListeConditions.get(iI)[iMODE_COMPARAISON] + "'%" + listListeConditions.get(iI)[iVALEUR] + "%'";
                        } else {
                            sSqlQuery = sSqlQuery + " AND " + listListeConditions.get(iI)[iID_DB] + listListeConditions.get(iI)[iMODE_COMPARAISON] + "'" + listListeConditions.get(iI)[iVALEUR] + "'";
                        }
                    }
                    if (listListeConditions.get(iI)[iMODE_TRIE] == null) {
                        listListeConditions.remove(iI);
                    } else {
                        iI++;
                    }
                }
            } else {
                if (listListeConditions.get(iI)[iVALEUR] != null) {
                    return null;
                }
                iI++;
            }
        }
        if (listListeConditions != null && listListeConditions.size() > 0) {
            sSqlQuery = sSqlQuery + " ORDER BY ";
            while (listListeConditions.size() > 0) {
                sSqlQuery = sSqlQuery + listListeConditions.get(0)[iID_DB] + listListeConditions.get(0)[iMODE_TRIE];
                listListeConditions.remove(0);
            }
        }
        if (limite > 0) {
            sSqlQuery = sSqlQuery + " LIMIT 0, " + String.valueOf(limite);
        }
        sSqlQuery = sSqlQuery + ";";
        return searchArticles(sSqlQuery);
    }
}
